<template>
<!-- import { parse } from 'querystring'; -->
  <div class="cart">
 <myheader titleName="购物车"></myheader>
    <div class="empty-cart" v-if="Cartstatus.length===0">
      <img src="" alt="" class="empty-cart-img">
      <div class="empty-cart-text">购物车还是空的</div>
      <div class="btn" @click="jumTo('goods_list')">去逛逛</div>
    </div>
    <div class="cart-container " v-for="(item,index) in Cartstatus" :key="item.id">
      <span class="iconfont toogle "
      @click="onetoogle(index)"
        :class="item.selectState?'icon-checkbox-marked-circ':'icon-checkbox-blank-circle-outline'
        "
      ></span>
      <div class="item-detail">
        <img :src="item.img" alt="">
        <div class="item-info">
          <h3>{{item.title}}</h3>
          <div class="pay">
            <div class="pay-price">￥{{item.price}}</div>
            <div class="edit-quantity">
              <p class="operate-btn iconfont icon-minus" @click="handminus(index)"></p>
              <p class="btn-input">{{item.num}}</p>
              <p class="operate-btn iconfont icon-plus" @click="handplus(index)"></p>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="cart-footer">
      <div class="cart-footer-left">
        <span class="iconfont toogle" :class="cartAllstat?'icon-checkbox-marked-circ':'icon-checkbox-blank-circle-outline'"   @click="tooglerm"></span>
        <span>全选</span></div>
      <div class="cart-footer-center">
        <span>合计：</span>
        <span class="total-price">￥{{Cartprice}}</span>
        </div>
      <div class="cart-footer-right">
        <span class="goto-pay">({{Cartcount}})结算</span></div>
    </div>
    <confirm v-model="isTrue" title="是否删除此条数据" @on-confirm="delThis(id)"></confirm>
  </div>
</template>
<script>
import { Confirm } from 'vux'
import myheader from '@/components/myheader'
export default {
  components: {
    Confirm,
    myheader
  },
  data () {
    return {
      id: this.$route.params.id,
      // id: -1,
      isTrue: false,
      // Cartlistpage: [], // 获取本地存储的数据
      Cartstatus: []
    }
  },
  created () {
    this.Cartstatus = JSON.parse(localStorage.getItem('mycart') || '[]')
  },
  methods: {
    jumTo (pathName) {
      this.$router.push({ name: pathName })
    },
    // 单按钮切换
    onetoogle (index) {
      this.Cartstatus[index].selectState = !this.Cartstatus[index].selectState
    },
    // 切换全部状态
    tooglerm () {
      // console.log(1)
      let liststatus = this.cartAllstat
      // // 全选时获取的全选按妞状态，是true
      // // 点击之后，状态变为全不选，小按钮和全选按钮的状态都false
      this.Cartstatus.map(item => {
        item.selectState = !liststatus
        // console.log(item.selectState)
      })
    },
    // 加数量
    handplus (index) {
      this.Cartstatus[index].num += 1
      // this.Cartstatus[index].num -= 1
    },
    // 减数量
    handminus (index) {
      if (this.Cartstatus[index].num === 1) {
        // console.log('111')
        this.isTrue = true
        this.id = index
      } else {
        this.Cartstatus[index].num -= 1
      }
    },
    // 实现删除
    delThis (index) {
      console.log(index)
      this.Cartstatus.splice(index, 1)
      console.log(this.Cartstatus)
    }
  },

  computed: {
    // 总价钱
    Cartprice () {
      let total = 0
      this.Cartstatus.map(item => {
        if (item.selectState) {
          total += item.num * item.price
        }
      })
      return total
    },
    // 总数量
    Cartcount () {
      let contolal = 0
      this.Cartstatus.map(item => {
        contolal += item.num
      })
      return contolal
    },
    // 创建一个全选全不选计算属性
    cartAllstat () {
      let selecAll = true
      this.Cartstatus.map(item => {
        if (item.selectState === false) {
          selecAll = false
        }
      })
      return selecAll
    }

  }

}
</script>

<style lang="scss" scoped>
.cart {
  height: 320px;
  margin-top: 45px;
  font-size: 14px;
  .empty-cart {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: flex-end;
    height: 100%;
    .empty-cart-img {
      height: 90px;
      width: 90px;
    }
    .empty-cart-text {
      font-size: 20px;
      color: #999;
      padding: 15px 0;
    }
    .btn {
      font-size: 20px;
      padding: 15px 55px;
      text-align: center;
      margin: 0 auto;
      border-radius: 10px;
      background: #ed601b;
      color: #fff;
    }
  }
  .cart-container {
    display: flex;
    align-items: center;
    margin: 5px;
    padding: 8px 10px;
    border: 1px solid #eeeeee;
    border-radius: 20px;
    .toogle {
      width: 20px;
      height: 20px;
      line-height: 20px;
      font-size: 20px;
    }
    .icon-checkbox-marked-circ {
      color: #ff5500;
    }
    .icon-checkbox-blank-circle-outline {
      color: #666666;
    }
  }
  .item-detail {
    display: flex;
    flex: 1;
    img {
      margin: 2px 0 2px 5px;
      width: 90px;
      height: 90px;
    }
    .item-info {
      width: 100%;
      padding: 5px 10px;
      display: flex;
      flex-direction: column;
      justify-content: space-around;
      h3 {
        font-size: 16px;
        font-weight: 400;
      }
      .pay {
        display: flex;
        justify-content: space-between;
        .pay-price {
          color: #ff5500;
          font-weight: bold;
          line-height: 30px;
        }
        .edit-quantity {
          display: flex;
          height: 30px;
          line-height: 30px;
          .operate-btn {
            padding: 0 10px;
            font-size: 20px;
            color: #bababa;
          }
          .btn-minus {
            font-size: 14px;
          }
        }
      }
    }
  }
  .cart-footer {
    display: flex;
    justify-content: space-between;
    position: fixed;
    bottom: 0;
    left: 0;
    height: 50px;
    line-height: 50px;
    width: 100%;
    border-top: 1px solid #e7e7e7;
    background-color: #fff;
    .cart-footer-left {
      display: flex;
      justify-content: cneter;
      align-items: center;
      span {
        display: block;
        height: 50px;
        padding: 0 5px;
      }
    }
    .cart-footer-center {
      .total-price {
        color: #ff5500;
        font-weight: bold;
      }
    }
    .cart-footer-right {
      display: flex;
      flex-direction: column;
      padding: 0 20px;
      background-color: #ff5500;
      .goto-pay {
        color: #fff;
      }
    }
    .icon-checkbox-marked-circ {
      color: #ff5500;
    }
    .icon-checkbox-blank-circle-outline {
      color: #666666;
    }
  }
}
</style>
